import {http} from '@/utils/http'

// 菜单接口类型定义
export interface MenuItem {
  id: number
  business: string
  title: string
  pid: number
  type: 'directory' | 'menu' | 'button'
  is_link: number
  link_url: string
  link_param: string
  components_url: string
  permission: string
  cache_status: number
  icon?: string
  sort: number
  status: number
  create_time?: number
  update_time?: number
  children?: MenuItem[]
}

export interface MenuForm {
  id?: number
  business: string
  title: string
  pid: number | null
  type: 'directory' | 'menu' | 'button'
  is_link: boolean
  link_url: string
  link_param: string
  components_url: string
  permission: string
  cache_status: number
  icon: string
  sort: number
  status: number
}

export interface FilterParams {
  status?: number
  cache_status?: number
  title?: string
  business?: string
}

// API 接口
export const menuApi = {
  // 获取菜单列表
  getMenuList(params: FilterParams) {
    return http.get<any>('/api/menu/index',{ params })
  },

  // 获取菜单树
  getMenuTree() {
    return http.get<any>('/api/menu/tree')
  },

  // 获取扁平菜单列表（用于树形选择器）
  getMenuFlatList(business: string) {
    return http.get<any>('/api/menu/flatList?business='+business)
  },

  // 获取菜单详情
  getMenuDetail(id: number) {
    return http.get<any>('/api/menu/read?id='+id)
  },

  // 新增菜单
  createMenu(data: MenuForm) {
    return http.post('/api/menu/save', data);
  },

  // 更新菜单
  updateMenu(id: number, data: MenuForm) {
    data.id = id;
    return http.post('/api/menu/update', data);
  },

  // 删除菜单
  deleteMenu(id: number) {
    return http.post('/api/menu/delete', {id:id});
  },

  // 更新菜单状态
  updateMenuStatus(id: number, status: number) {
    return http.post('/api/menu/update_status', {id:id,status:status});
  },

  // 获取模块
  select_system(){
    return http.get<any>('/api/menu/select_system')
  },

  // 获取动态路由
  routeMenu(params:any){
    return http.get<any>('/api/menu/routeMenu',{ params })
  }
}